using System;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Components.Security;
using System.Text;
using System.Net.Mail;
using ASPNETVN.PORTAL.Modules.NEWS.Components;

namespace ASPNETVN.PORTAL.Modules.NEWS.Controls
{
    public partial class Detail : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hlSignIn.NavigateUrl = "~/SignIn.aspx?ReturnUrl=" + Server.UrlEncode(Request.RawUrl);
                btnFeedback.Visible = PortalSecurity.CurrentUser.Identity.IsAuthenticated;
                pnlSignIn.Visible = !btnFeedback.Visible;

                Components.Item.IncreaseTotalViews(this.ItemID);
                Components.Item item = Components.Item.Load(this.ItemID);
                if (item != null)
                {
                    // Build page link.
                    PageLink pagelink = (PageLink)this.ParentControl.FindControl("MainPageLink");

                    int categoryID = item.CategoryID;
                    Category category = Category.Load(categoryID);
                    Category parentCategory = Category.Load(category.ParentID);

                    pagelink.AddLink(parentCategory.Name, "#");

                    string url = ResolveClientUrl(HttpUrlBuilder.BuildUrl(this.ModuleID, 0, ModulePages.Item, "categoryid={0}", category.ID));
                    pagelink.AddLink(category.Name, url);

                    pagelink.AddLink(item.Title, "#");


                    lblTitle.Text = item.Title;
                    lblApprovedDate.Text = item.ApprovedDate.ToLongDateString() + " - " + item.Authors;
                    lblTotalViews.Text = item.TotalViews.ToString("N0");
                    lblTotalComments.Text = item.TotalComments.ToString("N0");
                    ltrRate.Text = ImageHelper.BuildRateImage(item.Rate, Request);
                    lblMemo.Text = Server.HtmlDecode(item.Memo);
                }
            }
        }

        protected void dlRelationItem_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                Response.Redirect(HttpUrlBuilder.BuildUrl(this.ModuleID, Convert.ToInt32(e.CommandArgument), ModulePages.Detail));
            }
        }

        protected void btnSendMail_Click(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                string from = "webmaster@asp.net.vn";
                if (PortalSecurity.CurrentUser.Identity.IsAuthenticated)
                    from = PortalSecurity.CurrentUser.Identity.Email;


                StringBuilder body = new StringBuilder();
                body.Append("Xin chao!<br><br>");
                body.AppendFormat("Toi tim thay thong tin nay tren <a href='{0}'>ASP.NET VIETNAM</a>. Toi nghi ban quan tam thong tin nay.<br><br>", Global.ApplicationPath);

                string url = HttpUrlBuilder.BuildUrl(this.ModuleID, this.ItemID, ModulePages.Detail);
                url = url.Substring(2);
                url = Global.ApplicationPath + "/" + url;

                body.AppendFormat("Xem chi tiet tai day: <a href='{0}'>{0}</a><br><br>", url);

                body.AppendFormat("- Tieu de: <strong>{0}</strong><br><br>", lblTitle.Text);
                body.AppendFormat("- Thong diep: {0}<br><br>", txtMessage.Text);

                body.Append("<br>Chuc ban vui ve!");
                body.Append("<br><br><hr>");
                body.Append("<br><strong><a href='http://www.asp.net.vn'>ASP.NET VIET NAM</a> - Chia se kinh nghiem, nang cao ky nang.</strong>");

                string[] emails = txtEmails.Text.Split(';');
                foreach (string email in emails)
                {
                    MailHelper.SendMail(from, email, "", "ngothanhtung.it@gmail.com", "ASP.NET.VN - Thong tin chia se", body.ToString(), MailPriority.High);
                }
                txtEmails.Text = txtMessage.Text = string.Empty;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                Components.Comment comment = new Components.Comment();
                comment.ItemID = Convert.ToInt32(Request.QueryString["itemID"]);
                comment.Content = Server.HtmlEncode(txtContent.Text);
                comment.CreatedBy = PortalSecurity.CurrentUser.Identity.Email;
                comment.CreationTime = DateTime.Now;
                comment.Insert();

                txtContent.Text = string.Empty;
                dlComments.DataBind();
            }
        }

    }
}